import datetime
from typing import Optional

from sqlalchemy import String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.application.server.enums import ServerStatus
from src.infrastructure.db.models.base import Base


class VPNServer(Base):
    __tablename__ = "vpn_server"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    status: Mapped[ServerStatus] = mapped_column(server_default="active")
    name: Mapped[Optional[str]] = mapped_column(String(255))
    description: Mapped[Optional[str]] = mapped_column(String(255))
    ip_address: Mapped[str] = mapped_column(String(100), nullable=False)
    ssh_port: Mapped[int] = mapped_column(nullable=False)
    login: Mapped[str] = mapped_column(String(100), nullable=False)
    password: Mapped[str] = mapped_column(String(100), nullable=False)
    orders = relationship("Order", back_populates="vpn_server")
    was_added: Mapped[datetime.datetime] = mapped_column(server_default=func.now())

    def __str__(self):
        return self.ip_address

    def __repr__(self):
        return f"<VPN Server №{self.id} {self.ip_address}>"
